package com.qf.mapper;

import com.qf.pojo.Goods;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao{
    private QueryRunner runner = new QueryRunner(DBUtil.getDateSource());

    @Override
    public void insertGoods(Goods goods) {
        String sql ="insert into tb_goods(name,pubdate,picture,price,star,intro,tid) value(?,?,?,?,?,?,?) ";
        try {
            runner.update(sql, goods.getName(), goods.getPubdate(), goods.getPicture(), goods.getPrice(), goods.getStar(), goods.getIntro(), goods.getTid());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public List<Goods> findGoodALl() {
        String sql = "select tb_goods.*,tb_goods_type.name typeName from tb_goods " + "left join tb_goods_type  on tb_goods.tid = tb_goods_type.id";
//            String sql   = "select * from tb_goods";
        try {
            List<Goods> list = runner.query(sql, new BeanListHandler<Goods>(Goods.class));
            return list;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    @Override
    public Goods findById(Integer id) {
        String sql = "select * from tb_goods where id =?";
        try {
            Goods goods = runner.query(sql, new BeanHandler<Goods>(Goods.class), id);
            return goods;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
